package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.ext.MacArabic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for (int i = 4; i > intCode.toString().length(); i--) {

            unitCode = "0" + unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        //查询当前库存（可根据商品类别、商品编码或名称搜索）
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> goodsList = goodsDao.selectPageByCodeOrName(offSet, rows, codeOrName, goodsTypeId);
        int total = 0;
        for (int i = 0; i < goodsList.size(); i++) {
            total++;
        }
        map.put("total", total);
        map.put("rows", goodsList);
        return map;
    }

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        Integer offSite = (page - 1) * rows;
        List<Goods> goodsList = goodsDao.selectList(offSite, rows, goodsName, goodsTypeId);
        int total = 0;
        for (int i = 0; i < goodsList.size(); i++) {
            total++;
        }
        map.put("total", total);
        map.put("rows", goodsList);
        return map;
    }

    @Override
    public ServiceVO saveOrUpdate(Goods goods) {
        //保存或修改
        if (goods.getGoodsId() == null) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        if (goods != null) {
            goodsDao.updateById(goods);
        } else {
            goodsDao.insert(goods);
        }

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO removeGoods(Goods goods) {

        return null;
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        // 无库存商品列表展示（可以根据商品名称或编码查询）
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSite = (page - 1) * rows;

        List<Goods> noInventoryGoodsList = goodsDao.selectNoInventoryQuantity(offSite, rows, nameOrCode);
        map.put("total", noInventoryGoodsList.size());
        map.put("rows", noInventoryGoodsList);
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        // 有库存商品列表展示（可以根据商品名称或编码查询）
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSite = (page - 1) * rows;

        List<Goods> getHasInventoryQuantity = goodsDao.getHasInventoryQuantity(offSite, rows, nameOrCode);
        map.put("total", getHasInventoryQuantity.size());
        map.put("rows", getHasInventoryQuantity);
        return map;
    }


    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        //添加库存、修改数量或成本价
        if (goodsId == null || inventoryQuantity == null) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        Goods goods = goodsDao.selectById(goodsId);
        if (goods == null) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }

        goods.setInventoryQuantity(inventoryQuantity);
        goods.setPurchasingPrice(purchasingPrice);
        goodsDao.updateById(goods);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        //删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
        if (goodsId == null) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        Goods goods = goodsDao.selectById(goodsId);
        if (goods == null) {
            return new ServiceVO(ErrorCode.STORED_ERROR_CODE, ErrorCode.STORED_ERROR_MESS);
        }
        Integer state = goods.getState();
        if (state == 1) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        } else if (state == 2) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        if (state == 0) {
            goods.setInventoryQuantity(0);
            goodsDao.updateById(goods);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> listAlarm() {
        //查询所有 当前库存量 小于 库存下限的商品信息
        Map<String, Object> map = new HashMap<>();
        List<Goods> goodsList=goodsDao.selectStockLTNum();
        map.put("rows",goodsList);
        return map;
    }


}
